package set_;

import java.util.LinkedHashSet;
import java.util.Objects;

@SuppressWarnings({"all"})

public class LinkedHashSetExercise {
    public static void main(String[] args) {
        LinkedHashSet linkedHashSet = new LinkedHashSet();
        linkedHashSet.add(new Car("宝马" ,"3000000"));
        linkedHashSet.add(new Car("奥迪" ,"4000000"));
        linkedHashSet.add(new Car("奔驰" ,"2530000"));
        linkedHashSet.add(new Car("大众" ,"111111"));
        linkedHashSet.add(new Car("法拉利" ,"45555500"));
        linkedHashSet.add(new Car("保时捷" ,"32222221"));
        System.out.println(linkedHashSet);
    }
}

/**
 * Car有属性值name 和 price
 * 有相同的元素就就不能添加
 */
class Car{
    private String name;
    private String price;

    public Car(String name, String price) {
        this.name = name;
        this.price = price;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPrice() {
        return price;
    }

    public void setPrice(String price) {
        this.price = price;
    }

    @Override
    public String toString() {
        return "\nCar{" +
                "name='" + name + '\'' +
                ", price='" + price + '\'' +
                '}';
    }
    //重写equals和HashCode值
    //当name和price相同时，返回相同的hashCode值，equals返回t
    //

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Car car = (Car) o;
        return Objects.equals(name, car.name) && Objects.equals(price, car.price);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, price);
    }
}